
const mongoose = require("mongoose");
const Schema = mongoose.Schema;
class mongo_models{
  static Get_ProduceDetail_Model(onlySchema=false){
    let schema= new Schema({
      taskmid: {type: String,index: true},
      tar: {type: String},
      result: {type: String},
      state:{type: Number}, //0 1 2 3 4
      hasresult:{type: Number}, // 0 1
      message:{type: String},
      time:{type: Number},
    })
    if(onlySchema==true){
      return schema
    }
    else{
      let ModelName=`ProducesDetails`
      let model=global["Mongo_Connection"].models[ModelName]
      if(model){
        return model
      }
      else{
        return global["Mongo_Connection"].model(ModelName,schema,ModelName);
      }
    }
  }
  static Get_ProducesTask_Model(onlySchema=false){
    let schema= new Schema({
      batch: {type: String,index: true},
      orderid: {type: String,index: true},
      username: {type: String,index: true},
      s3path: {type: String,index: true},
      foldername: {type: String,index: true},
      msg: {type: String},
      data: {type: String},
      ack:{type: Number},// 1 uack 2ack
      copy:{type: Number},// 1 ucopy 2copyed
      state:{type: Number}, //1 2 3 4  1查询中 2下载数据 3下载完成并检查完毕 4生产运行中 5结束 6结束并成功 7错误
      message:{type: String},//提示消息
      taskid:{type: String}, //任务的id
      hasresult:{type: Number}, // 0 1  是否包含结果
      atime:{type: Number}, //任务添加时间
      stime:{type: Number}, //开始时间
      etime:{type: Number}, //结束时间
      options:{
        
      }
    })
    if(onlySchema==true){
      return schema
    }
    else{
      let ModelName=`ProduceTask`
      let model=global["Mongo_Connection"].models[ModelName]
      if(model){
        return model
      }
      else{
        return global["Mongo_Connection"].model(ModelName,schema,ModelName);
      }
    }
  }
  static async  UpdateDetail(taskmid,tar,result,state,hasresult,message){
    let conditions={}
    conditions["taskmid"]=taskmid
    conditions["tar"]=tar
    let set={}
    set["taskmid"]=taskmid
    set["tar"]=tar
    set["result"]=result
    set["state"]=state
    set["hasresult"]=hasresult
    set["message"]=message
    set["time"]=(new Date()).getTime()
    console.log(`update:${taskmid}  ${tar} ...`)
    let ProduceDetail_Model=mongo_models.Get_ProduceDetail_Model()
    await ProduceDetail_Model.updateOne(
      conditions,
      {
        $set: set
      },
      { upsert: true }
    );
    return true
  }
  static async  GetDetail(options){
    let {_id,id,tar}=options
    let conditions={}
    if(id){
      conditions["taskmid"]=id
    }
    if(_id){
      conditions["taskmid"]=_id
    }
    if(tar){
      conditions["tar"]=tar
    }
    let ProduceDetail_Model=mongo_models.Get_ProduceDetail_Model()
    let query = await ProduceDetail_Model.find(conditions)
    return query
  }
  static async  GetTask(options){
    let {_id,id,batch,orderid,username,state,taskid,foldername}=options
    let conditions={}
    if(id){
      conditions["_id"]=id
    }
    if(_id){
      conditions["_id"]=_id
    }
    if(batch){
      conditions["batch"]=batch
    }
    if(orderid){
      conditions["orderid"]=orderid
    }
    if(foldername){
      conditions["foldername"]=foldername
    }
    if(taskid){
      conditions["taskid"]=taskid
    }
    if(username){
      conditions["username"]=username
    }
    if(state>=1){
      conditions["state"]=state
    }
    let ProducesTask_Model=mongo_models.Get_ProducesTask_Model()
    let query = await ProducesTask_Model.findOne(conditions)
    if(query){
      query=query?.toJSON()
      if(query["_id"]){
        query.mongo_id=query["_id"]?.toString()
      }
    }
    return query
  }
  static async  DeleteTask(id){
    let conditions={}
    if(id){
      conditions["_id"]=id
    }
    let ProducesTask_Model=mongo_models.Get_ProducesTask_Model()
    let count=await ProducesTask_Model?.countDocuments(conditions)
    if(count==1){
      let update_data=await ProducesTask_Model.deleteOne(conditions);
      return true
    }
    else{
      return false
    }
  }
  static async  SetStat(id,state){
    let conditions={}
    if(id){
      conditions["_id"]=id
    }
    let set={}
    if(state>=1){
      set["state"]=state
      if(state==1){
        set["message"]="无"
      }
      else if(state==5 || state==6){
        set["etime"]=(new Date()).getTime()
      }
    }
    let ProducesTask_Model=mongo_models.Get_ProducesTask_Model()
    let count=await ProducesTask_Model?.countDocuments(conditions)
    if(count==1){
      if(state>=1){
        let update_data=await ProducesTask_Model.updateOne(
          conditions,
          {
            $set: set
          },
          { upsert: true }
        );
      }
    }
    else{
      return false
    }
    return true
  }
  static async  GetTasks(options){
    let {_id,id,batch,orderid,username,state,taskid,mongo_id}=options
    state=state-0
    let conditions={}
    if(id){
      conditions["_id"]=id
    }
    if(_id){
      conditions["_id"]=_id
    }
    if(batch){
      conditions["batch"]=batch
    }
    if(orderid){
      conditions["orderid"]=orderid
    }
    if(taskid){
      conditions["taskid"]=taskid
    }
    if(username){
      conditions["username"]=username
    }
    if(state>=1){
      conditions["state"]=state
    }
    let ProducesTask_Model=mongo_models.Get_ProducesTask_Model()
    let query = await ProducesTask_Model.find(conditions)
    let new_query=[]
    for(let i=0;i<query?.length;i++){
      let one_query=query[i]
      one_query=one_query?.toJSON()
      if(one_query["_id"]){
        one_query.mongo_id=one_query["_id"].toString()
      }
      if(mongo_id){
        let short_name=one_query.mongo_id?.substr(-4)
        if(short_name==mongo_id){
          new_query.push(one_query)
        }
      }
      else{
        new_query.push(one_query)
      }
      
    }
    return new_query
  }
  static async  UpdateTask(options){
    let {id,batch,orderid,username,s3path,foldername,msg,data,ack,copy,state,message,taskid,hasresult,atime,stime,etime}=options
    let conditions={}
    if(id){
      conditions["_id"]=id
    }
    if(batch){
      conditions["batch"]=batch
    }
    if(orderid){
      conditions["orderid"]=orderid
    }
    if(username){
      conditions["username"]=username
    }
    if(foldername){
      conditions["foldername"]=foldername
    }
    let set={}
    if(batch){
      set["batch"]=batch
    }
    if(orderid){
      set["orderid"]=orderid
    }
    if(username){
      set["username"]=username
    }
    if(s3path){
      set["s3path"]=s3path
    }
    if(foldername){
      set["foldername"]=foldername
    }
    if(msg){
      set["msg"]=msg
    }
    if(data){
      set["data"]=data
    }
    if(ack>=0){
      set["ack"]=ack
    }
    if(copy>=0){
      set["copy"]=copy
    }
    if(taskid){
      set["taskid"]=taskid
    }
    if(state>=0){
      set["state"]=state
    }
    if(message !=undefined){
      set["message"]=message
    }
    if(hasresult>=0){
      set["hasresult"]=hasresult
    }
    if(atime>=0){
      set["atime"]=atime
    }
    if(stime>=0){
      set["stime"]=stime
    }
    if(etime>=0){
      set["etime"]=etime
    }
    let ProducesTask_Model=mongo_models.Get_ProducesTask_Model()
    let update_data=await ProducesTask_Model.updateOne(
      conditions,
      {
        $set: set
      },
      { upsert: true }
    );
    let query = await mongo_models.GetTask(conditions)
    return query
  }
}
module.exports = mongo_models;